﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Linq;
using Triples.Model;

namespace Triples {

    public class FindAllSetsController : Controller {

        private const int MaxFoundSetsSize = 6;
        private const int BoardSize = 12;
        private readonly Board InternalBoard;
        private readonly FindAllSetsScoreCard InternalScoreBoard;


        public ObservableCollection<CardSet> FoundSets {
            get;
            private set;
        }


        public Timer GameTimer {
            get;
            private set;
        }


        public override Board GameBoard {
            get { return InternalBoard; }
        }


        public override ScoreCard ScoreBoard {
            get { return InternalScoreBoard; }
        }


        public FindAllSetsController() {
            GameTimer = new Timer(TimeSpan.FromMinutes (3));
            FoundSets = new ObservableCollection<CardSet>();
            InternalScoreBoard = new FindAllSetsScoreCard (GameTimer);
            InternalBoard = ConstructBoard();

            GameTimer.Finished += OnGameTimerFinished;
            GameTimer.Start();
        }


        private void OnGameTimerFinished(object sender, EventArgs args) {
            GameTimer.Stop();
            ReportGameOver();
        }
        

        protected Board ConstructBoard() {
            Board board = new Board();
            Deck deck = new Deck(Model.BoardSeeds.GenerateSeed());

            for (int i = 0; i < BoardSize; i++) {
                board.PlaceCard(GetNextCard (deck));
            }

            return board;
        }


        protected override void ProcessSelectedCard(IPlayableCard card) {
            int count = this.SelectedSet.Count();

            if (0 == count || 1 == count) {

                foreach (IPlayableCard c in this.GameBoard) {
                    c.IsEnabled = true;
                }
            }

            if (1 == count) {
                foreach (IPlayableCard c in this.SelectedSet) {

                    foreach (CardSet set in this.FoundSets) {
                        if (set.Contains(c)) {

                            foreach (IPlayableCard d in set) {
                                if (!this.SelectedSet.Contains(d)) {
                                    d.IsEnabled = false;
                                }
                            }
                        }
                    }
                }
            }
        }


        protected override bool ProcessFoundSet(CardSet set) {

            if (!FoundSets.Contains(set)) {
                FoundSets.Add(set);

                if (MaxFoundSetsSize == FoundSets.Count) {
                    GameTimer.Stop();
                    IsGameComplete = true;
                }

                return true;
            }

            return false;
        }
    }
}
